home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / StringDialog.c < prev    next >
Text File  |  1994-08-15  |  9KB  |  288 lines

  1. /* StringDialog.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "StringDialog.h"
  31. #include "Screen.h"
  32. #include "EventLoop.h"
  33. #include "SimpleButton.h"
  34. #include "TextEdit.h"
  35. #include "Memory.h"
  36. #include "DataMunging.h"
  37. #include "Alert.h"
  38. #include "Menus.h"
  39.  
  40.  
  41. #define HEIGHT (76)
  42. #define WIDTH (350)
  43.  
  44. #define TEXTLEFT (10)
  45. #define TEXTTOP (13)
  46. #define TEXTWIDTH (120)
  47. #define TEXTHEIGHT (20)
  48.  
  49. #define EDITLEFT (150)
  50. #define EDITTOP (10)
  51. #define EDITWIDTH (190)
  52. #define EDITHEIGHT (20)
  53.  
  54. #define OKWIDTH (93)
  55. #define OKHEIGHT (21)
  56. #define OKLEFT (WIDTH / 4 - OKWIDTH / 2)
  57. #define OKTOP (44)
  58.  
  59. #define CANCELWIDTH (OKWIDTH)
  60. #define CANCELHEIGHT (OKHEIGHT)
  61. #define CANCELLEFT (3 * WIDTH / 4 - CANCELWIDTH / 2)
  62. #define CANCELTOP (OKTOP)
  63.  
  64.  
  65. typedef struct
  66.     {
  67.         WinType*                            ScreenID;
  68.         SimpleButtonRec*            OKButton;
  69.         TextEditRec*                    TextField;
  70.         char*                                    StaticPrompt;
  71.         SimpleButtonRec*            CancelButton;
  72.     } StringDialogRec;
  73.  
  74.  
  75. static void        LocalUpdateRoutine(StringDialogRec* Window)
  76.     {
  77.         CheckPtrExistence(Window);
  78.         SetClipRect(Window->ScreenID,0,0,WIDTH,HEIGHT);
  79.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->StaticPrompt,
  80.             StrLen(Window->StaticPrompt),TEXTLEFT,TEXTTOP,ePlain);
  81.         RedrawSimpleButton(Window->OKButton);
  82.         RedrawSimpleButton(Window->CancelButton);
  83.         TextEditFullRedraw(Window->TextField);
  84.     }
  85.  
  86.  
  87. /* present a dialog box displaying the string and allowing the user to make */
  88. /* changes. if the user clicks OK, then True is returned, else False is returned. */
  89. MyBoolean        DoStringDialog(char* Prompt, char** TheString,
  90.                             MenuItemType* MCut, MenuItemType* MPaste, MenuItemType* MCopy,
  91.                             MenuItemType* MUndo, MenuItemType* MSelectAll, MenuItemType* MClear)
  92.     {
  93.         StringDialogRec*        Window;
  94.         MyBoolean                        DoItFlag;
  95.  
  96.         /* make sure the string they gave us really exists */
  97.         CheckPtrExistence(*TheString);
  98.  
  99.         Window = (StringDialogRec*)AllocPtrCanFail(sizeof(StringDialogRec),"StringDialogRec");
  100.         if (Window == NIL)
  101.             {
  102.              FailurePoint1:
  103.                 AlertHalt("There is not enough memory to display the dialog box.",NIL);
  104.                 return False;
  105.             }
  106.  
  107.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  108.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WIDTH),
  109.             DialogTopEdge(HEIGHT),WIDTH,HEIGHT,(void (*)(void*))&LocalUpdateRoutine,Window);
  110.         if (Window->ScreenID == 0)
  111.             {
  112.              FailurePoint2:
  113.                 ReleasePtr((char*)Window);
  114.                 goto FailurePoint1;
  115.             }
  116.  
  117.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKLEFT,OKTOP,
  118.             OKWIDTH,OKHEIGHT);
  119.         if (Window->OKButton == NIL)
  120.             {
  121.              FailurePoint3:
  122.                 KillWindow(Window->ScreenID);
  123.                 goto FailurePoint2;
  124.             }
  125.         SetDefaultButtonState(Window->OKButton,True);
  126.  
  127.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELLEFT,
  128.             CANCELTOP,CANCELWIDTH,CANCELHEIGHT);
  129.         if (Window->CancelButton == NIL)
  130.             {
  131.              FailurePoint4:
  132.                 DisposeSimpleButton(Window->OKButton);
  133.                 goto FailurePoint3;
  134.             }
  135.  
  136.         Window->TextField = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),9,
  137.             EDITLEFT,EDITTOP,EDITWIDTH,EDITHEIGHT);
  138.         if (Window->TextField == NIL)
  139.             {
  140.              FailurePoint5:
  141.                 DisposeSimpleButton(Window->CancelButton);
  142.                 goto FailurePoint4;
  143.             }
  144.         TextEditNewRawData(Window->TextField,*TheString,""/*shouldn't be a linefeed*/);
  145.         TextEditDoMenuSelectAll(Window->TextField);
  146.         EnableTextEditSelection(Window->TextField);
  147.  
  148.         /* allocation is finished; copy over parameters */
  149.         Window->StaticPrompt = Prompt;
  150.  
  151.         /* do our local event loop */
  152.         while (1)
  153.             {
  154.                 OrdType                            X;
  155.                 OrdType                            Y;
  156.                 ModifierFlags                Modifiers;
  157.                 MenuItemType*                MenuItem;
  158.                 char                                KeyPress;
  159.  
  160.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  161.                     {
  162.                         default:
  163.                             break;
  164.                         case eCheckCursor:
  165.                             if (TextEditIBeamTest(Window->TextField,X,Y))
  166.                                 {
  167.                                     SetIBeamCursor();
  168.                                 }
  169.                              else
  170.                                 {
  171.                                     SetArrowCursor();
  172.                                 }
  173.                             goto NoEventPoint;
  174.                             break;
  175.                         case eNoEvent:
  176.                          NoEventPoint:
  177.                             TextEditUpdateCursor(Window->TextField);
  178.                             break;
  179.                         case eMenuStarting:
  180.                             EnableMenuItem(MPaste);
  181.                             if (TextEditIsThereValidSelection(Window->TextField))
  182.                                 {
  183.                                     EnableMenuItem(MCut);
  184.                                     EnableMenuItem(MCopy);
  185.                                     EnableMenuItem(MClear);
  186.                                 }
  187.                             EnableMenuItem(MSelectAll);
  188.                             if (TextEditCanWeUndo(Window->TextField))
  189.                                 {
  190.                                     EnableMenuItem(MUndo);
  191.                                 }
  192.                             break;
  193.                         case eMenuCommand:
  194.                             if (MenuItem == MPaste)
  195.                                 {
  196.                                     TextEditDoMenuPaste(Window->TextField);
  197.                                 }
  198.                             else if (MenuItem == MCut)
  199.                                 {
  200.                                     TextEditDoMenuCut(Window->TextField);
  201.                                 }
  202.                             else if (MenuItem == MCopy)
  203.                                 {
  204.                                     TextEditDoMenuCopy(Window->TextField);
  205.                                 }
  206.                             else if (MenuItem == MClear)
  207.                                 {
  208.                                     TextEditDoMenuClear(Window->TextField);
  209.                                 }
  210.                             else if (MenuItem == MUndo)
  211.                                 {
  212.                                     TextEditDoMenuUndo(Window->TextField);
  213.                                     TextEditShowSelection(Window->TextField);
  214.                                 }
  215.                             else if (MenuItem == MSelectAll)
  216.                                 {
  217.                                     TextEditDoMenuSelectAll(Window->TextField);
  218.                                 }
  219.                             else
  220.                                 {
  221.                                     EXECUTE(PRERR(AllowResume,
  222.                                         "DoStringDialog: Undefined menu option chosen"));
  223.                                 }
  224.                             break;
  225.                         case eKeyPressed:
  226.                             if (KeyPress == 13)
  227.                                 {
  228.                                     char*                                    RawTemp;
  229.  
  230.                                     FlashButton(Window->OKButton);
  231.                                  OKButtonClickedPoint:
  232.                                     RawTemp = TextEditGetRawData(Window->TextField,
  233.                                         ""/*shouldn't be a linefeed*/);
  234.                                     if (RawTemp != NIL)
  235.                                         {
  236.                                             ReleasePtr(*TheString);
  237.                                             *TheString = RawTemp;
  238.                                             DoItFlag = True;
  239.                                         }
  240.                                      else
  241.                                         {
  242.                                             DoItFlag = False;
  243.                                         }
  244.                                     goto AllDonePoint;
  245.                                 }
  246.                             else if (KeyPress == eCancelKey)
  247.                                 {
  248.                                     FlashButton(Window->CancelButton);
  249.                                     DoItFlag = False;
  250.                                     goto AllDonePoint;
  251.                                 }
  252.                             else
  253.                                 {
  254.                                     TextEditDoKeyPressed(Window->TextField,KeyPress,Modifiers);
  255.                                 }
  256.                             break;
  257.                         case eMouseDown:
  258.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  259.                                 {
  260.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  261.                                         {
  262.                                             goto OKButtonClickedPoint;
  263.                                         }
  264.                                 }
  265.                             else if (TextEditHitTest(Window->TextField,X,Y))
  266.                                 {
  267.                                     TextEditDoMouseDown(Window->TextField,X,Y,Modifiers);
  268.                                 }
  269.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  270.                                 {
  271.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  272.                                         {
  273.                                             DoItFlag = False;
  274.                                             goto AllDonePoint;
  275.                                         }
  276.                                 }
  277.                             break;
  278.                     }
  279.             }
  280.      AllDonePoint:
  281.         DisposeTextEdit(Window->TextField);
  282.         DisposeSimpleButton(Window->OKButton);
  283.         DisposeSimpleButton(Window->CancelButton);
  284.         KillWindow(Window->ScreenID);
  285.         ReleasePtr((char*)Window);
  286.         return DoItFlag;
  287.     }
  288.